{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "编写一个自定义函数计算并返回第n阶调和数",
   "id": "95643022ce99766"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import math\n",
    "from fractions import Fraction\n",
    "\n",
    "\n",
    "def a_harmonic_fraction_with_an_expression(n):\n",
    "    # if n is negative, return 0 and string representation 0\n",
    "    if n < 1:\n",
    "        return Fraction(0), \"0\"\n",
    "\n",
    "    # initialize result and expression\n",
    "    expression = \"1\"\n",
    "    result = Fraction(1, 1)\n",
    "\n",
    "    # loop through numbers from 2 to n, start from 2\n",
    "    for i in range(2, n + 1):\n",
    "        result += Fraction(1, i)\n",
    "        expression += f\" + 1/{i}\"\n",
    "\n",
    "    return result, expression\n",
    "\n",
    "\n",
    "number = 8\n",
    "harmonic, expression = a_harmonic_fraction_with_an_expression(number)\n",
    "\n",
    "print(f\"The harmonic fraction of {n} is {harmonic}\")\n",
    "print(f\"expression: {expression}\")"
   ],
   "id": "initial_id",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "编写一个函数，求n个数的乘积，返回所有参数的乘积，参数个数不限",
   "id": "d79a72dd07c950dd"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "def function(*args):\n",
    "    # initialize result to 1\n",
    "    result = 1\n",
    "\n",
    "    # loop through all arguments\n",
    "    for arg in args:\n",
    "        result *= arg\n",
    "\n",
    "    return args, result\n",
    "\n",
    "\n",
    "args, result = function(1, 2, 3, 4, 5)\n",
    "\n",
    "# test the function\n",
    "print(f\"arguments tuple: {args}\")\n",
    "print(f\"result: {result}\")"
   ],
   "id": "e60216c1667e7cbd",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "编写函数求斐波那契数列中第n个数的值。斐波那契数列指的是从第3项开始，每一项都等于前两项之和。",
   "id": "533f56ba8df69bab"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "def fibonacci(n):\n",
    "    # check if n is negative or 0\n",
    "    if n < 0 or n == 0:\n",
    "        return 0\n",
    "\n",
    "    # check if n is 1 or 2\n",
    "    elif n == 1 or n == 2:\n",
    "        return 1\n",
    "\n",
    "    # recursion to calculate the nth number\n",
    "    else:\n",
    "        a, b = 0, 1\n",
    "        for _ in range(2, n):\n",
    "            a, b = b, a + b\n",
    "\n",
    "        return b\n",
    "\n",
    "\n",
    "# test the function\n",
    "number = 7\n",
    "result = fibonacci(n)\n",
    "print(f\"The {n} number in the Fibonacci sequence is: {result}\")"
   ],
   "id": "754a1ac7f3e05f4c",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "编写函数实现二分查找法。二分查找法又称折半查找，是一种非常快速的查找算法，它对要查找的序列有两个要求，一是该序列必须是有序的（即该序列中的所有元素都是按照大小关系列），二是该序列必须是顺序存储的；请分别使用递归思想和非递归思想实现",
   "id": "be5ce82dd16a98c"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import time\n",
    "\n",
    "\n",
    "def binary_search_iterative(array, target):\n",
    "    # record the start time\n",
    "    start_time = time.time()\n",
    "\n",
    "    # initialize left point and right point\n",
    "    left, right = 0, len(array) - 1\n",
    "\n",
    "    # loop until left point is less than or equal to right point\n",
    "    while left <= right:\n",
    "\n",
    "        # calculate the middle value point\n",
    "        middle = left + (right - left) // 2\n",
    "\n",
    "        # if middle point‘s value is equal to target, return the value\n",
    "        if array[middle] == target:\n",
    "            return middle, time.time() - start_time\n",
    "\n",
    "        # if middle point's value is greater than target, move the right point to middle point - 1, otherwise, move the left point to middle point + 1\n",
    "        elif array[middle] < target:\n",
    "            left = middle + 1\n",
    "        else:\n",
    "            right = middle - 1\n",
    "\n",
    "            # if target is not found, return the number -1 to indicate that it was not found\n",
    "    return -1, time.time() - start_time\n",
    "\n",
    "\n",
    "def binary_search_recursive(array, target, left, right):\n",
    "    # record the start time\n",
    "    start_time = time.time()\n",
    "\n",
    "    # if left point is greater than right point, return the number -1 indicate that target is not found\n",
    "    if left > right:\n",
    "        return -1, 0.0\n",
    "\n",
    "    middle = left + (right - left) // 2\n",
    "    if array[middle] == target:\n",
    "        return middle, time.time() - start_time\n",
    "\n",
    "    # Recursive methods finding the target, it's so cool!\n",
    "    elif array[middle] < target:\n",
    "        return binary_search_recursive(array, target, middle + 1, right)\n",
    "    else:\n",
    "        return binary_search_recursive(array, target, left, middle - 1)\n",
    "\n",
    "\n",
    "array = [11, 22, 33, 44, 55, 66, 77, 88, 99]\n",
    "error_target = 100\n",
    "success_target = 44\n",
    "\n",
    "# test the function named binary_search_iterative\n",
    "iterative_method_error_result, iterative_method_error_time = binary_search_iterative(array, error_target)\n",
    "print(\n",
    "    f\"[iterative method] [error test] Index of target {target} is {iterative_method_error_result}, time cost: {iterative_method_error_time}s\")\n",
    "\n",
    "iterative_method_success_result, iterative_method_success_time = binary_search_iterative(array, success_target)\n",
    "print(\n",
    "    f\"[iterative method] [success test] Index of target {target} is {iterative_method_success_result}, time cost: {iterative_method_success_time}s\")\n",
    "\n",
    "# test the function named binary_search_recursive\n",
    "recursive_method_error_result, recursive_method_error_time = binary_search_recursive(array, error_target, 0,\n",
    "                                                                                     len(array) - 1)\n",
    "print(\n",
    "    f\"[recursive method] [error test] Index of target {target} is {recursive_method_error_result}, time cost: {recursive_method_error_time}s\")\n",
    "\n",
    "recursive_method_success_result, recursive_method_success_time = binary_search_recursive(array, success_target, 0,\n",
    "                                                                                         len(array) - 1)\n",
    "print(\n",
    "    f\"[recursive method] [success test] Index of target {target} is {recursive_method_success_result}, time cost: {recursive_method_success_time}s\")"
   ],
   "id": "24af11d85ec8b441",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "编写程序完成求任意多个数的最大公约数",
   "id": "67f4e2c7ca30a5d2"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from math import gcd\n",
    "from functools import reduce\n",
    "\n",
    "\n",
    "def gcd_of_numbers(numbers):\n",
    "    # use reduce function to calculate the greatest common divisor, enm..., it's simple method to implement\n",
    "    # The elements in the parameter sequence are accumulated.\n",
    "    return reduce(gcd, numbers)\n",
    "\n",
    "\n",
    "# another method to calculate the greatest common divisor\n",
    "def gcd_of_multiple_numbers(*args):\n",
    "    def gcd_f(a, b):\n",
    "        while b:\n",
    "            a, b = b, a % b\n",
    "        return a\n",
    "\n",
    "    result = args[0]\n",
    "    # loop through the remaining arguments\n",
    "    for number in args[1:]:\n",
    "        result = gcd_f(result, number)\n",
    "\n",
    "    return result\n",
    "\n",
    "\n",
    "numbers = [6, 4, 2]\n",
    "print(f\"The greatest common divisor of tuple {numbers} is {gcd_of_numbers(numbers)}\")\n",
    "print(\n",
    "    f\"The greatest common divisor of list {numbers} is {gcd_of_multiple_numbers(*numbers)}\")  # * is used to unpack the list into separate arguments"
   ],
   "id": "4f119715b9925497",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "使用生成器函数每次输出10个斐波那契数列元素",
   "id": "33509beb356c4e22"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "def fibonacci_generator():\n",
    "    # initialize the first two elements\n",
    "    a, b = 0, 1\n",
    "    while True:\n",
    "        yield a  # generator value\n",
    "        a, b = b, a + b\n",
    "\n",
    "\n",
    "def fibonacci_generator_result():\n",
    "    yield from fibonacci_generator()\n",
    "\n",
    "\n",
    "print(\"The first 10 elements of fibonacci series are:\", end=\" \")\n",
    "for number in fibonacci_generator_result() and range(10):\n",
    "    print(number, end=\" \")"
   ],
   "id": "bf3809d7cc39c2d4",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "从一个学生信息表中读取数据，每页只显示10条记录",
   "id": "297063f829e8ca01"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import csv\n",
    "\n",
    "\n",
    "def read_student_info(path, size):\n",
    "    with open(path, 'r', encoding='utf-8') as file:\n",
    "        reader = csv.reader(file)\n",
    "        next(reader)\n",
    "        for i in range(size):\n",
    "            row = next(reader)\n",
    "            print(f\"ID: {row[0]}, Name: {row[1]}, Age: {row[2]}, Gender: {row[3]}, Class: {row[4]}\")\n",
    "\n",
    "\n",
    "# read the student information\n",
    "read_student_info('data/student_info.csv', 10)"
   ],
   "id": "bb0abd679187e828",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "请使用闭包求x的y次方",
   "id": "ed084fe19e2794b2"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import math\n",
    "\n",
    "\n",
    "def power(x):\n",
    "    def inner(y):\n",
    "        return math.pow(x, y)\n",
    "\n",
    "    return inner\n",
    "\n",
    "\n",
    "# test the power function\n",
    "power_2 = power(2)\n",
    "print(power_2(3))"
   ],
   "id": "5e6ee0333571cfb2",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "装饰器的应用，有以下三个求和函数，在不修改源程序的情况下分别输出他们的计算运行时间",
   "id": "a78d324b20ae253c"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "import time\n",
    "\n",
    "\n",
    "# decorator function to measure the time taken by sum function\n",
    "def time_cost(func):\n",
    "    def wrapper(*args, **kwargs):\n",
    "        start_time = time.time()\n",
    "        result = func(*args, **kwargs)\n",
    "        end_time = time.time()\n",
    "        print(f\"Time cost: {end_time - start_time}s\")\n",
    "        return result\n",
    "\n",
    "    return wrapper\n",
    "\n",
    "\n",
    "@time_cost\n",
    "def add_integer(*args):\n",
    "    sum = 0\n",
    "    for element in args:\n",
    "        sum += element\n",
    "    print(f\"[add_integer] sum is {sum}\")\n",
    "\n",
    "\n",
    "@time_cost\n",
    "def add_float(*args):\n",
    "    sum = 0.0\n",
    "    for element in args:\n",
    "        sum += element\n",
    "    print(f\"[add_float] sum is {sum}\")\n",
    "\n",
    "\n",
    "@time_cost\n",
    "def add_complex(*args):\n",
    "    comsum = 0.0 + 0.0j\n",
    "    for element in args:\n",
    "        comsum += element\n",
    "    print(f\"[add_complex] sum is {comsum}\")\n",
    "\n",
    "\n",
    "# test the sum function with different parameters\n",
    "add_integer(1, 2, 3, 4, 5)\n",
    "add_float(1.1, 2.2, 3.3, 4.4, 5.5)\n",
    "add_complex(1.1 + 0.1j, 2.2 + 0.2j, 3.3 + 0.3j, 4.4 + 0.4j, 5.5 + 0.5j)"
   ],
   "id": "65097d97c2991f30",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "编写一个函数，实现单例模式。单例模式(Singleton Pattern)是一种常用的软件设计模式，该模式的主要目的是确保某一个类只有一个实例存在。",
   "id": "23d1b415a9a2f89c"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "class SingletonPattern:\n",
    "    __instance = None\n",
    "\n",
    "    # private constructor to prevent create instance from outside\n",
    "    def __new__(cls, *args, **kwargs):\n",
    "        # check if instance already exists\n",
    "        if not cls.__instance:\n",
    "            cls.__instance = super().__new__(cls, *args, **kwargs)\n",
    "\n",
    "        # return the instance\n",
    "        return cls.__instance\n",
    "\n",
    "\n",
    "# test the singleton pattern class\n",
    "s1 = SingletonPattern()\n",
    "s2 = SingletonPattern()\n",
    "print(s1 is s2)  # if same instance, print true, otherwise false"
   ],
   "id": "ab4c2d75e2f8b252",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "验证一个给定的字符串是否为有效的手机号码。",
   "id": "14c2980ffc1fe07c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-29T06:20:51.035708Z",
     "start_time": "2024-05-29T06:20:03.292089Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import re\n",
    "\n",
    "\n",
    "def validate_phone_number(phone_number):\n",
    "    pattern = r'^(\\+?86-?)?1[3-9]\\d{9}$'\n",
    "    if re.match(pattern, phone_number):\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "\n",
    "while True:\n",
    "    phone_number = input(\"请输入电话号码：\")\n",
    "    if validate_phone_number(phone_number):\n",
    "        print(\"电话号码符合要求！\")\n",
    "        break\n",
    "    else:\n",
    "        print(\"电话号码不符合要求，请输入正确格式的电话号码。\")"
   ],
   "id": "c8cadb531d79c0d0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "电话号码不符合要求，请输入正确格式的电话号码。\n",
      "电话号码不符合要求，请输入正确格式的电话号码。\n",
      "电话号码不符合要求，请输入正确格式的电话号码。\n",
      "电话号码符合要求！\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "学生管理系统，包括学生信息的添加、删除、修改和查询功能。",
   "id": "db4e4e244efde9d8"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-29T06:22:38.547849Z",
     "start_time": "2024-05-29T06:22:38.534925Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class Student:\n",
    "    def __init__(self, name, id, age):\n",
    "        self.name = name\n",
    "        self.id = id\n",
    "        self.age = age\n",
    "\n",
    "\n",
    "class StudentManager:\n",
    "    def __init__(self):\n",
    "        self.students = {}\n",
    "\n",
    "    def add_student(self, student):\n",
    "        self.students[student.id] = student\n",
    "\n",
    "    def get_student(self, student_id):\n",
    "        return self.students.get(student_id, None)\n",
    "\n",
    "    def list_students(self):\n",
    "        for student_id, student in self.students.items():\n",
    "            print(f\"学生 ID: {student_id}, 姓名: {student.name}, 年龄: {student.age}\")\n",
    "\n",
    "\n",
    "def main():\n",
    "    manager = StudentManager()\n",
    "\n",
    "    manager.add_student(Student(\"Alice\", \"S001\", 20))\n",
    "    manager.add_student(Student(\"Bob\", \"S002\", 22))\n",
    "\n",
    "    student_id = \"S001\"\n",
    "    student = manager.get_student(student_id)\n",
    "    print(f\"学生 ID: {student_id}, 姓名: {student.name}, 年龄: {student.age}\")\n",
    "\n",
    "    manager.list_students()\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ],
   "id": "bc91c7d7ce0c8a2b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学生 ID: S001, 姓名: Alice, 年龄: 20\n",
      "学生 ID: S001, 姓名: Alice, 年龄: 20\n",
      "学生 ID: S002, 姓名: Bob, 年龄: 22\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "验证日期格式",
   "id": "a0d5b2fa3fac5745"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-29T06:31:42.994576Z",
     "start_time": "2024-05-29T06:31:26.765773Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import re\n",
    "\n",
    "\n",
    "def validate_date(date):\n",
    "    pattern = r'^\\d{4}-\\d{2}-\\d{2}$'\n",
    "    if re.match(pattern, date):\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "\n",
    "\n",
    "while True:\n",
    "    date = input(\"请输入日期（YYYY-MM-DD）：\")\n",
    "    if validate_date(date):\n",
    "        print(f\"日期为{date}\")\n",
    "        break\n",
    "    else:\n",
    "        print(f\"{date}日期不符合要求，请输入正确格式的日期（YYYY-MM-DD）。\")"
   ],
   "id": "c89141e2bc055636",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0000-0888-0000日期不符合要求，请输入正确格式的日期（YYYY-MM-DD）。\n",
      "日期为2024-01-01\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "验证密码",
   "id": "e56017767c3497e0"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-29T06:33:09.026620Z",
     "start_time": "2024-05-29T06:33:09.018619Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import re\n",
    "def validate_password(password):\n",
    "    pattern = r\"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$\"\n",
    "    if re.match(pattern, password):\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "password = \"Abc@1234\"\n",
    "if validate_password(password):\n",
    "    print(\"密码符合要求\")\n",
    "else:\n",
    "    print(\"密码不符合要求\")\n",
    "password = \"Abc3\"\n",
    "if validate_password(password):\n",
    "    print(\"密码符合要求\")\n",
    "else:\n",
    "    print(\"密码不符合要求\")\n"
   ],
   "id": "e303acecacec79a0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "密码符合要求\n",
      "密码不符合要求\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "提取邮箱地址并验证邮箱地址",
   "id": "89881bc19cd8a71f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-29T06:37:36.244033Z",
     "start_time": "2024-05-29T06:37:36.229408Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import re\n",
    "def is_valid_email(email):\n",
    "    email_regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$'\n",
    "    if re.match(email_regex, email):\n",
    "        return True\n",
    "    else:\n",
    "        return False\n",
    "def extract_and_validate_emails(text):\n",
    "    emails = extract_emails(text)\n",
    "    valid_emails = [email for email in emails if is_valid_email(email)]\n",
    "    return valid_emails\n",
    "def extract_emails(text):\n",
    "    email_pattern = r'\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b'\n",
    "    emails = re.findall(email_pattern, text)\n",
    "    return emails\n",
    "\n",
    "text = \"Hello email@example.com 或者 support@example.net\"\n",
    "valid_emails = extract_and_validate_emails(text)\n",
    "print(\"有效邮箱：\", valid_emails)"
   ],
   "id": "ac7197e3c0db6895",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "有效邮箱： ['email@example.com', 'support@example.net']\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "动态时钟（copy）",
   "id": "1dac3bb9631135d"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "from turtle import *\n",
    "from datetime import *\n",
    "\n",
    "\n",
    "def skip(step):\n",
    "    penup()\n",
    "    forward(step)\n",
    "    pendown()\n",
    "\n",
    "\n",
    "def mk_hand(name, length):\n",
    "    reset()\n",
    "    skip(-length * 0.1)\n",
    "    begin_poly()\n",
    "    forward(length * 1.1)\n",
    "    end_poly()\n",
    "    hand_form = get_poly()\n",
    "    register_shape(name, hand_form)\n",
    "\n",
    "\n",
    "def init():\n",
    "    global sec_hand, min_hand, hur_hand, printer\n",
    "    mode(\"logo\")\n",
    "    mk_hand(\"secHand\", 125)\n",
    "    mk_hand(\"minHand\", 130)\n",
    "    mk_hand(\"hurHand\", 90)\n",
    "    sec_hand = Turtle()\n",
    "    sec_hand.shape(\"secHand\")\n",
    "    min_hand = Turtle()\n",
    "    min_hand.shape(\"minHand\")\n",
    "    hur_hand = Turtle()\n",
    "    hur_hand.shape(\"hurHand\")\n",
    "    for hand in sec_hand, min_hand, hur_hand:\n",
    "        hand.shapesize(1, 1, 3)\n",
    "        hand.speed(0)\n",
    "    printer = Turtle()\n",
    "    printer.hideturtle()\n",
    "    printer.penup()\n",
    "\n",
    "\n",
    "def setup_clock(radius):\n",
    "    reset()\n",
    "    pensize(7)\n",
    "    for i in range(60):\n",
    "        skip(radius)\n",
    "        if i % 5 == 0:\n",
    "            forward(20)\n",
    "            skip(-radius - 20)\n",
    "        else:\n",
    "            dot(5)\n",
    "            skip(-radius)\n",
    "        right(6)\n",
    "\n",
    "\n",
    "def week(t):\n",
    "    week = [\"星期一\", \"星期二\", \"星期三\", \"星期四\", \"星期五\", \"星期六\", \"星期日\"]\n",
    "    return week[t.weekday()]\n",
    "\n",
    "\n",
    "def date(t):\n",
    "    return \"%s %d %d\" % (t.year, t.month, t.day)\n",
    "\n",
    "\n",
    "def tick():\n",
    "    t = datetime.today()\n",
    "    second = t.second + t.microsecond * 0.000001\n",
    "    minute = t.minute + second / 60.0\n",
    "    hour = t.hour + minute / 60.0\n",
    "    sec_hand.setheading(6 * second)\n",
    "    min_hand.setheading(6 * minute)\n",
    "    hur_hand.setheading(30 * hour)\n",
    "    tracer(False)\n",
    "    printer.forward(65)\n",
    "    printer.write(week(t), align=\"center\", font=(\"Courier\", 14, \"bold\"))\n",
    "    printer.back(130)\n",
    "    printer.write(date(t), align=\"center\", font=(\"Courier\", 14, \"bold\"))\n",
    "    printer.home()\n",
    "    tracer(True)\n",
    "    ontimer(tick, 100)\n",
    "\n",
    "\n",
    "tracer(False)\n",
    "init()\n",
    "setup_clock(160)\n",
    "tracer(True)\n",
    "tick()\n",
    "mainloop()"
   ],
   "id": "efcacb2534043e27"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
